﻿window.alert = function (name) {
    let iframe = document.createElement("IFRAME");
    iframe.style.display = "none";
    document.documentElement.appendChild(iframe);
    window.frames[0].window.alert(name);
    iframe.parentNode.removeChild(iframe);
};
window.confirm = function (message) {
    let iframe = document.createElement("IFRAME");
    iframe.style.display = "none";
    iframe.setAttribute("src", 'data:text/plain,');
    document.documentElement.appendChild(iframe);
    let alertFrame = window.frames[0];
    let result = alertFrame.window.confirm(message);
    iframe.parentNode.removeChild(iframe);
    return result;
};
//查询科室
selectAjax("FollowUpGroup/SelectByResponsibleDepartment", "keshi");
selectAjax("FollowUpGroup/SelectByResponsibleDepartment", "kesi");
//查询角色
selectAjax("Role/SelectAll", "role");
//查询科室
selectAjax("FollowUpGroup/SelectByResponsibleDepartment", "keshis");
//查询角色
selectAjax("Role/SelectAll", "roles");

function selectAjax(url, attribute) {
    $.ajax({
        url: "http://localhost:8989/hospital/" + url,
        type: "post",
        dataType: "json",
        success: function (data) {
            for (let dataKey in data) {

                if (attribute == "keshi") {
                    let department = data[dataKey].responsibleDepartment;
                    let id = data[dataKey].id;
                    $("<option value='" + id + "'>" + department + "</option>").prependTo("#" + attribute);
                    $('#' + attribute).selectpicker('refresh');
                }
                if (attribute == "keshis") {
                    let department = data[dataKey].responsibleDepartment;
                    let id = data[dataKey].id;
                    $("<option value='" + id + "'>" + department + "</option>").prependTo("#" + attribute);
                    $('#' + attribute).selectpicker('refresh');
                }
                if (attribute == "kesi") {
                    let department = data[dataKey].responsibleDepartment;
                    let id = data[dataKey].id;
                    $("#" + attribute).append("<option class='option1' value=" + id + ">" + department + "</option>");
                }
                if (attribute == "role") {
                    let id = data[dataKey].id;
                    let roleDesc = data[dataKey].roleDesc;
                    $("#" + attribute).after("<option class='option' value=" + id + ">" + roleDesc + "</option>");
                }
                if (attribute == "roles") {
                    let id = data[dataKey].id;
                    let roleDesc = data[dataKey].roleDesc;
                    if (roleDesc != "管理员") {
                        $("#" + attribute).after("<option class='options' value=" + id + ">" + roleDesc + "</option>");
                    }
                }
            }
        }
    });
}

//全局变量
let overallSituation;
$(function () {
    //获取页面的每个按钮
    let btns = $(".btn_active");
    //获取内容盒子
    let contents = $(".content");
    //遍历每个按钮为其添加点击事件
    for (var i = 0; i < btns.length; i++) {
        btns[i].index = i;
        btns[i].onclick = function () {
            //对当前点击的按钮赋予active类名及显示当前按钮对应的内容
            for (var j = 0; j < btns.length; j++) {
                btns[j].className = btns[j].className.replace(' active', '').trim();
                contents[j].className = contents[j].className.replace(' show', '').trim();
            }
            this.className = this.className + ' active';
            contents[this.index].className = contents[this.index].className + ' show';
        };
    }
});

angular.module('quanXian', []).controller('quanXianView', function ($scope) {
    /**
     * checkbox全选/全不选
     */
    //给全选的复选框添加事件
    $(document).on("click", "#all", function () {
        // this 全选的复选框
        var userids = this.checked;
        //获取name=box的复选框 遍历输出复选框
        $("input[name=box]").each(function () {
            this.checked = userids;
        });
    });

    //给name=box的复选框绑定单击事件 在angular属性里jquery点击事件不触发 解决方法用on
    $(document).on("click", "input[name=box]", function () {
        //获取选中复选框长度
        var length = $("input[name=box]:checked").length;
        //未选中的长度
        var len = $("input[name=box]").length;
        if (length == len) {
            $("#all").get(0).checked = true;
        } else {
            $("#all").get(0).checked = false;
        }
    });

    let pageData = {
        page: 1,
        pageNum: 5
    };

    //编辑方法
    $scope.selectByUserId = function () {
        let result = "";
        let count = 0;
        $(".check").each(function () {
            if ($(this).is(':checked')) {
                result += $(this).val();
                count++;
            }
        });
        if (count > 1 || count == 0) {
            alert("请选择一条数据进行编辑");
            return;
        } else {
            $("#myModal2").modal("show");
            $("#div").html("<span></span>");
            $("#buttons").html("<button onclick='update()' type='button' class='btn btn-info'>修改</button><button type='button' class='btn btn-default' data-dismiss='modal'>取消</button>");
        }
        $(".select").find("option").attr("selected", false);
        $.ajax({
            url: "http://localhost:8989/hospital/User/SelectByUserId",
            type: "post",
            dataType: "json",
            data: {"id": result},
            success: function (data) {
                $scope.$apply(function () {
                    $scope.datas = data;
                    //全局变量
                    overallSituation = $scope.datas;
                    //数组
                    let array = new Array();
                    //科室选中项
                    for (let responsibleDepartmentKey in $scope.datas.responsibleDepartment) {
                        //下拉菜单选中状态
                        array.push($scope.datas.responsibleDepartment[responsibleDepartmentKey].id);
                    }
                    $(".selectpicker").selectpicker('val', array);

                    $(".select").find("option").attr("selected", false);
                    $(".select2").find("option").attr("selected", false);
                    //角色选中项
                    for (let roleKey in $scope.datas.roles) {
                        $(".select").find("option[value='" + $scope.datas.sex + "']").prop("selected", true);
                        $(".select2").find("option[value='" + $scope.datas.roles[roleKey].id + "']").prop("selected", true);
                    }
                });
            }
        });
    };
    //调用查询所有权限方法
    jurisdictionSelectAll();

    //查询所有权限方法
    function jurisdictionSelectAll() {
        $.ajax({
            url: "http://localhost:8989/hospital/Jurisdiction/SelectAll",
            type: "post",
            dataType: "json",
            success: function (data) {
                $scope.$apply(function () {
                    $scope.jurisdiction = data;
                })
            }
        });
    }

    //添加方法
    $scope.insert = function () {
        //错误信息
        let state;
        //style样式
        let style = " style='color: red'";
        let styles = "style='color: #00cc03' class='glyphicon glyphicon-warning-sign'";
        //获取账户名
        let username = $("#username").val();
        //获取姓名
        let name = $("#name").val();
        //获取电话
        let phone = $("#phone").val();
        //获取邮箱
        let email = $("#email").val();
        //获取密码
        let password = $("#password").val();
        //获取性别
        let sex = $(".sex:selected").val();
        //获取科室多选值
        let department = $(".selectpickers").selectpicker('val');
        //获取角色选择值
        let role = $(".option:selected").val();
        //封装数据
        let data = {
            username: username,
            name: name,
            phone: phone,
            email: email,
            password: password,
            sex: sex
        };
        //正则表达式
        if ((data.username == "" || data.username == null) && (data.name == "" || data.name == null) && (data.phone == "" || data.phone == null) && (data.email == "" || data.email == null) && (data.password == "" || data.password == null) && (data.sex == undefined || data.sex == "" || data.sex == null)) {
            state = "请填写添加的信息";
            $("#divs").html("<span style='color: #f8c815' class='glyphicon glyphicon-warning-sign'></span>&nbsp;<span" + style + ">" + state + "</span>");
            return;
        } else if (data.username == "" || data.username == null) {
            state = "请填写账户名";
            $("#divs").html("<span style='color: #f8c815' class='glyphicon glyphicon-warning-sign'></span>&nbsp;<span" + style + ">" + state + "</span>");
            return;
        } else if (!(/^\w+.{3,20}$/.test(data.username))) {
            state = "请正确填写账户名";
            $("#divs").html("<span style='color: #f8c815' class='glyphicon glyphicon-warning-sign'></span>&nbsp;<span" + style + ">" + state + "</span>");
            return;
        } else if (data.password == null || data.password == "") {
            state = "请填写密码";
            $("#divs").html("<span style='color: #f8c815' class='glyphicon glyphicon-warning-sign'></span>&nbsp;<span" + style + ">" + state + "</span>");
            return;
        } else if (!(/^.{6,20}$/.test(password))) {
            state = "请正确填写密码(6-20)位";
            $("#divs").html("<span style='color: #f8c815' class='glyphicon glyphicon-warning-sign'></span>&nbsp;<span" + style + ">" + state + "</span>");
            return;
        } else if (data.phone == null || data.phone == "") {
            state = "请填写手机号";
            $("#divs").html("<span style='color: #f8c815' class='glyphicon glyphicon-warning-sign'></span>&nbsp;<span" + style + ">" + state + "</span>");
            return;
        } else if (!(/^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/.test(phone))) {
            state = "请正确填写手机号";
            $("#divs").html("<span style='color: #f8c815' class='glyphicon glyphicon-warning-sign'></span>&nbsp;<span" + style + ">" + state + "</span>");
            return;
        } else if (data.email == null || data.email == "") {
            state = "请填写邮箱";
            $("#divs").html("<span style='color: #f8c815' class='glyphicon glyphicon-warning-sign'></span>&nbsp;<span" + style + ">" + state + "</span>");
            return;
        } else if (!(/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(email))) {
            state = "请正确填写邮箱";
            $("#divs").html("<span style='color: #f8c815' class='glyphicon glyphicon-warning-sign'></span>&nbsp;<span" + style + ">" + state + "</span>");
            return;
        } else if (data.name == "" || data.name == null) {
            state = "请填写姓名";
            $("#divs").html("<span style='color: #f8c815' class='glyphicon glyphicon-warning-sign'></span>&nbsp;<span" + style + ">" + state + "</span>");
            return;
        } else if (!(/^[\u4e00-\u9fa5]{0,}$/.test(name))) {
            state = "请正确填写姓名";
            $("#divs").html("<span style='color: #f8c815' class='glyphicon glyphicon-warning-sign'></span>&nbsp;<span" + style + ">" + state + "</span>");
            return;
        } else if (role == undefined || role == null || role == "") {
            state = "请选择角色";
            $("#divs").html("<span style='color: #f8c815' class='glyphicon glyphicon-warning-sign'></span>&nbsp;<span" + style + ">" + state + "</span>");
            return;
        } else if (sex == undefined || sex == null || sex == "") {
            state = "请选择性别";
            $("#divs").html("<span style='color: #f8c815' class='glyphicon glyphicon-warning-sign'></span>&nbsp;<span" + style + ">" + state + "</span>");
            return;
        } else if (department == null) {
            state = "请选择科室";
            $("#divs").html("<span style='color: #f8c815' class='glyphicon glyphicon-warning-sign'></span>&nbsp;<span" + style + ">" + state + "</span>");
            return;
        } else {
            state = "";
            $("#divs").html("<span></span>");
        }
        $.ajax({
            url: "http://localhost:8989/hospital/User/Insert",
            type: "post",
            data: data,
            dataType: "json",
            success: function (status) {
                if (status == true) {
                    selectByUserId(data.name);
                } else {
                    state = "操作失败";
                    $("#divs").html("<div>" + styles + "</div>&nbsp;<div " + style + ">" + state + "</div>");
                }
            },
            error: function () {
                state = "操作失败";
                $("#divs").html("<div>" + styles + "</div>&nbsp;<div " + style + ">" + state + "</div>");
            }
        });

        function selectByUserId(name) {
            $.ajax({
                url: "http://localhost:8989/hospital/User/SelectByUserName",
                type: "post",
                dataType: "json",
                data: {
                    "name": name
                },
                success: function (data) {
                    for (let dataKey in data) {
                        for (let datumKey in data[dataKey]) {
                            insertRole(data[dataKey][datumKey].id);
                        }
                    }
                }
            });
        }

        //添加用户对应的角色信息
        function insertRole(userId) {
            $.ajax({
                url: "http://localhost:8989/hospital/UserRole/Insert",
                type: "post",
                dataType: "json",
                data: {
                    "userId": userId,
                    "roleId": role
                },
                success: function (status) {
                    if (status == true) {
                        insertDepartment(userId);
                    }
                }
            });
        }

        //添加用户对应的科室信息
        function insertDepartment(userId) {
            $.ajax({
                url: "http://localhost:8989/hospital/UserDepartment/InsertDepartment",
                type: "post",
                dataType: "json",
                data: {
                    "userId": userId,
                    "departmentId": department
                },
                success: function (status) {
                    if (status == true) {
                        setTimeout(function () {
                            $("#myModal1").modal('hide');
                            state = "操作成功";
                            $("#column").html("<div " + styles + "></div>&nbsp;" + "<span style='color: #2aabd2'>" + state + "</span>");
                            $('body').on('hidden.bs.modal', '#myModal1', function () {
                                $('#myModal1 input').val('');
                                $('#myModal1 select').prop("value", '请选择');
                                $("#keshi").selectpicker('deselectAll');
                                setTimeout(function () {
                                    $("#divs").html('<span></span>');
                                    $("#column").html("<div></div>");
                                }, 2000);
                            });
                        }, 500);
                        $scope.SelectAll();
                    } else {
                        state = "操作失败";
                        $("#divs").html("<div>" + styles + "</div>&nbsp;<div " + style + ">" + state + "</div>");
                    }
                },
                error: function () {
                    state = "操作失败";
                    $("#divs").html("<div>" + styles + "</div>&nbsp;<div " + style + ">" + state + "</div>");
                }
            });
        }
    }
    //删除方法
    $scope.remove = function (status) {
        let result = "";
        let count = 0;
        $(".check").each(function () {
            if ($(this).is(':checked')) {
                result += $(this).val() + ",";
                count++;
            }
        });
        if (status != null && count != 0) {
            if (!confirm("确定删除这" + count + "条角色信息?")) {
                return;
            }
            bulkDelete();

            //批量删除方法
            function bulkDelete() {
                $.ajax({
                    url: "http://localhost:8989/hospital/Role/BulkDelete",
                    type: "post",
                    dataType: "json",
                    data: {"id": result},
                    success: function (status) {
                        if (status == true) {
                            //全选多选框设置未选中状态
                            $("#all").attr("checked", false);
                            if (pageData.page != 1) pageData.page = 1;
                            //调用表格数据与分页数据函数
                            $scope.SelectAll();
                        } else {
                            //全选多选框设置未选中状态
                            $("#all").attr("checked", false);
                            if (pageData.page != 1) pageData.page = 1;
                            $scope.SelectAll();
                        }
                    }
                });
            }
        } else {
            alert("请至少选择一条信息进行删除")
        }
    };
    // 上一页/下一页
    $scope.page1 = function (status) {
        //上一页
        if (status == "-1") {
            if (pageData.page > 1) {
                pageData.page = pageData.page - 1;
                //调用表格数据与分页数据函数
                $scope.SelectAll();
            }
        }
        //下一页
        if (status == "+1") {
            if (pageData.page < $scope.page.remaining_pages) {
                pageData.page = pageData.page + 1;
                //调用表格数据与分页数据函数
                $scope.SelectAll();
            }
        }
        //尾页
        if (status == "" || status == null) {
            pageData.page = $scope.page.remaining_pages;
            //调用表格数据与分页数据函数
            $scope.SelectAll();
        }
        //跳转页
        if (status == "jump") {
            let val = $("#JumpPage").val();
            if (val == null || val == "") {
                alert("请输入跳转页");
                return;
            }
            if (val > $scope.page.remaining_pages) {
                alert("没有下一页了");
                return;
            }
            pageData.page = val;
            //调用表格数据与分页数据函数
            $scope.SelectAll();
        }
    };
    $scope.optionUserJurisdiction = function (userId) {
        $("#userid").prop("value", userId);
        //获取选中的权限值
        let attribute = ".jurisdictions:checked";
        $(attribute).prop("checked", false);
        $.ajax({
            url: "http://localhost:8989/hospital/Jurisdiction/SelectByUserJurisdiction",
            type: "post",
            dataType: "json",
            data: {"userId": userId},
            success: function (data) {
                let jurisdiction;
                for (let dataKey in data) {
                    jurisdiction = data[dataKey];
                    $(".icheck-info").find("input[value='" + jurisdiction.jid + "']").prop("checked", true);
                }
            }
        });
        $.ajax({
            url: "http://localhost:8989/hospital/User/SelectByUserId",
            type: "post",
            dataType: "json",
            data: {"id": userId},
            success: function (data) {
                let datas;
                for (let dataKey in data) {
                    datas = data[dataKey]
                }
                let jurisdiction;
                for (let datasKey in datas) {
                    jurisdiction = datas[datasKey].jurisdictions;
                }
                for (let jurisdictionKey in jurisdiction) {
                    let array = [1, 2, 3, 4];
                    for (let i = 0; i < array.length; i++) {
                        $(".icheck-info").find("input[value='" + array[i] + "']").attr("disabled", 'disabled')
                    }
                    $(".icheck-info").find("input[value='" + jurisdiction[jurisdictionKey].id + "']").prop("checked", true);
                }
            }
        });
    }
    //获取选中行id
    $scope.submitOptionUserJurisdiction = function () {
        //获取用户id
        let userId = $("#userid").val();
        //获取选中的权限值
        let attribute = ".jurisdictions:checked";
        let result = "";
        $(".jurisdictions").each(function () {
            if ($(this).is(':checked')) {
                result += $(this).val() + ",";
            }
        });
        //数据
        let data = {
            "userId": userId,
            "jurisdictionId": result
        }
        $("#myModal").modal('hide');
        $.ajax({
            url: "http://localhost:8989/hospital/Jurisdiction/InsertOrUpdate",
            type: "post",
            data: data,
            dataType: "json",
            success: function (state) {
                if (state == true) {
                    $("#column").html("<span style='color: #2ecc71'>设定成功</span>")
                } else {
                    $("#column").html("<span style='color: red'>设定失败</span>")
                }
                setTimeout(function () {
                    $("#column").empty();
                }, 5000)
            }
        });
        $(attribute).prop("checked", false);
    }
    //查所有方法
    $scope.SelectAll = function () {
        $.ajax({
            url: "http://localhost:8989/hospital/UserRole/SelectAll",
            type: "post",
            data: pageData,
            dataType: "json",
            success: function (data) {
                $scope.$apply(function () {
                    let datas;
                    for (let dataKey in data) {
                        datas = data[dataKey];
                        $scope.page = datas;
                        if ($scope.page.count == 0) {
                            $("#column").html("<tr><td style=\"color: red\">没有数据</td></tr>");
                            $(".data").empty();
                        } else {
                            $("#column").empty();
                        }
                        for (let i = 0; i <= datas.length - 1; i++) {
                            $scope.data = datas;
                        }
                    }
                    //查询用户角色与权限信息
                    selectUserRole("UserRole/SelectAll", "authorizedUser", $scope.page);
                });
                //加载loading
                $(".spinner").css("display", "none");
            }, error: function () {
                $("#column").html("<span style='color: #f8c815' class='glyphicon glyphicon-warning-sign'></span>&nbsp;<span style='color: red'>加载失败</span>");
                $(".spinner").css("display", "none");
            }
        });
        //更改获取的id值
        page("shangyes");
        page("xiayes");
        page("weiyes");
        page("tiaozhuanyes");

        page("shangye");
        page("xiaye");
        page("weiye");
        page("tiaozhuanye");
    };

    //修改触发的点击事件
    function page(state) {
        if (state == "shangyes") {
            $("#" + state).css("display", "none");
        } else if (state == "xiayes") {
            $("#" + state).css("display", "none");
        } else if (state == "weiyes") {
            $("#" + state).css("display", "none");
        } else if (state == "tiaozhuanyes") {
            $("#" + state).css("display", "none");
        } else if (state == "shangye") {
            $("#" + state).css("display", "");
        } else if (state == "xiaye") {
            $("#" + state).css("display", "");
        } else if (state == "weiye") {
            $("#" + state).css("display", "");
        } else if (state == "tiaozhuanye") {
            $("#" + state).css("display", "");
        }
    }

    //调用查所有方法
    $scope.SelectAll();

    $scope.selectByUserRoleLike = function () {
        //错误信息
        let state;
        //style样式
        let style = " style='color: red'";
        let roleName = $("#roleName").val();
        let roleRemarks = $("#roleRemarks").val();
        //正则表达式判断
        if ((roleName == null || roleName == "") && (roleRemarks == null || roleRemarks == "")) {
            state = "请至少填写一个条件";
            $("#state").html("<span style='color: #f8c815' class='glyphicon glyphicon-warning-sign'></span>&nbsp;<span" + style + ">" + state + "</span>");
            return;
        } else if (!(/^[\u4e00-\u9fa5]{0,}$/.test(roleName))) {
            state = "请正确填写角色名称";
            $("#state").html("<span style='color: #f8c815' class='glyphicon glyphicon-warning-sign'></span>&nbsp;<span" + style + ">" + state + "</span>");
            return;
        } else if (!(/^[\u4e00-\u9fa5]{0,}$/.test(roleRemarks))) {
            state = "请正确填写备注";
            $("#state").html("<span style='color: #f8c815' class='glyphicon glyphicon-warning-sign'></span>&nbsp;<span" + style + ">" + state + "</span>");
            return;
        } else {
            state = "检索成功";
            $("#state").html("<span style='color: #00ee00' class='glyphicon glyphicon-warning-sign'></span>&nbsp;<span style='color: #0e90d2'>" + state + "</span>");
        }
        let data = {
            "roleName": roleName,
            "roleRemarks": roleRemarks
        };
        $scope.TablesAndPaginationLike(data);
    };

    // 上一页/下一页
    $scope.page2 = function (status) {
        //上一页
        if (status == "-1") {
            if (pageData.page > 1) {
                pageData.page = pageData.page - 1;
                $scope.selectByUserRoleLike();
            }
        }
        //下一页
        if (status == "+1") {
            if (pageData.page < $scope.page.remaining_pages) {
                pageData.page = pageData.page + 1;
                $scope.selectByUserRoleLike();
            }
        }
        //尾页
        if (status == "" || status == null) {
            pageData.page = $scope.page.remaining_pages;
            if (pageData.page != 0) {
                $scope.selectByUserRoleLike();
            }
        }
        //跳转页
        if (status == "jump") {
            let val = $("#JumpPage").val();
            if (val == null || val == "") {
                alert("请输入跳转页");
                return;
            }
            if (val > $scope.page.remaining_pages) {
                alert("没有下一页了");
                return;
            }
            pageData.page = val;
            //条件查询方法
            $scope.selectByUserRoleLike();
        }
    };
    //状态值
    let status = 0;
    $scope.TablesAndPaginationLike = function (data) {
        //更改获取的id值
        pages("shangye");
        pages("xiaye");
        pages("weiye");
        pages("tiaozhuanye");
        //更改获取的id值
        pages("shangyes");
        pages("xiayes");
        pages("weiyes");
        pages("tiaozhuanyes");
        pages("");
        $.ajax({
            url: "http://localhost:8989/hospital/UserRole/SelectByUserRoleLike",
            type: "post",
            data: {
                "page": pageData.page,
                "pageNum": pageData.pageNum,
                "roleName": data.roleName,
                "roleRemarks": data.roleRemarks
            },
            dataType: "json",
            success: function (data) {
                $scope.$apply(function () {
                    let datas;
                    for (let dataKey in data) {
                        datas = data[dataKey];
                        $scope.page = datas;
                        if ($scope.page.count == 0) {
                            $("#column").html("<tr><td style=\"color: red\">没有数据</td></tr>");
                            $(".data").empty();
                        } else {
                            $("#column").empty();
                        }
                        for (let i = 0; i <= datas.length - 1; i++) {
                            $scope.data = datas;
                        }
                    }
                    //查询用户角色与权限信息
                    selectUserRole("UserRole/SelectAll", "authorizedUser", $scope.page);
                });
                //加载loading
                $(".spinner").css("display", "none");
            }, error: function () {
                $("#column").html("<span style='color: #f8c815' class='glyphicon glyphicon-warning-sign'></span>&nbsp;<span style='color: red'>加载失败</span>");
                $(".spinner").css("display", "none");
            }
        });

        //修改触发的点击事件
        function pages(state) {
            if (state == "shangye") {
                $("#" + state).css("display", "none");
            } else if (state == "xiaye") {
                $("#" + state).css("display", "none");
            } else if (state == "weiye") {
                $("#" + state).css("display", "none");
            } else if (state == "tiaozhuanye") {
                $("#" + state).css("display", "none");
            } else if (state == "shangyes") {
                $("#" + state).css("display", "");
            } else if (state == "xiayes") {
                $("#" + state).css("display", "");
            } else if (state == "weiyes") {
                $("#" + state).css("display", "");
            } else if (state == "tiaozhuanyes") {
                $("#" + state).css("display", "");
            } else if (state == "") {
                status += 1;
                if (status == 1) pageData.page = 1;
            }
        }
    };


    //查询用户角色与权限信息
    function selectUserRole(url, attribute, pageNum) {
        $.ajax({
            url: "http://localhost:8989/hospital/" + url,
            type: "post",
            data: {
                page: 1,
                pageNum: pageNum.count
            },
            dataType: "json",
            success: function (data) {
                if (attribute == "authorizedUser") {
                    let datas;
                    for (let i = 0; i < data.length - 1; i++) {
                        datas = data[i]
                        for (let datasKey in datas) {
                            if (datas[datasKey].name && datas[datasKey].id != undefined) {
                                $("#" + attribute).append("<option class='authorized1' value=" + datas[datasKey].id + ">" + datas[datasKey].name + " / " + datas[datasKey].id + "</option>")
                            }
                        }
                    }
                }
            }
        });
    }
});

//修改方法
function update() {
    //错误信息
    let state;
    //style样式
    let style = " style='color: red'";
    let styles = "style='color: #00cc03' class='glyphicon glyphicon-warning-sign'";
    //获取账户名
    let username = $("#usernames").val();
    //获取姓名
    let name = $("#names").val();
    //获取电话
    let phone = $("#phones").val();
    //获取邮箱
    let email = $("#emails").val();
    //获取性别
    let sex = $(".sexs:selected").val();
    //获取科室多选值
    let department = $(".selectpicker").selectpicker('val');
    //获取角色选择值
    let role = $(".options:selected").val();
    //封装数据
    let data = {
        username: username,
        name: name,
        phone: phone,
        email: email,
        sex: sex
    };
    //正则表达式
    if ((data.username == "" || data.username == null) && (data.name == "" || data.name == null) && (data.phone == "" || data.phone == null) && (data.email == "" || data.email == null) && (data.password == "" || data.password == null) && (data.sex == undefined || data.sex == "" || data.sex == null)) {
        state = "请填写添加的信息";
        $("#div").html("<span style='color: #f8c815' class='glyphicon glyphicon-warning-sign'></span>&nbsp;<span" + style + ">" + state + "</span>");
        return;
    } else if (data.username == "" || data.username == null) {
        state = "请填写账户名";
        $("#div").html("<span style='color: #f8c815' class='glyphicon glyphicon-warning-sign'></span>&nbsp;<span" + style + ">" + state + "</span>");
        return;
    } else if (!(/^\w+.{3,20}$/.test(data.username))) {
        state = "请正确填写账户名";
        $("#div").html("<span style='color: #f8c815' class='glyphicon glyphicon-warning-sign'></span>&nbsp;<span" + style + ">" + state + "</span>");
        return;
    } else if (data.phone == null || data.phone == "") {
        state = "请填写手机号";
        $("#div").html("<span style='color: #f8c815' class='glyphicon glyphicon-warning-sign'></span>&nbsp;<span" + style + ">" + state + "</span>");
        return;
    } else if (!(/^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/.test(phone))) {
        state = "请正确填写手机号";
        $("#div").html("<span style='color: #f8c815' class='glyphicon glyphicon-warning-sign'></span>&nbsp;<span" + style + ">" + state + "</span>");
        return;
    } else if (data.email == null || data.email == "") {
        state = "请填写邮箱";
        $("#div").html("<span style='color: #f8c815' class='glyphicon glyphicon-warning-sign'></span>&nbsp;<span" + style + ">" + state + "</span>");
        return;
    } else if (!(/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(email))) {
        state = "请正确填写邮箱";
        $("#div").html("<span style='color: #f8c815' class='glyphicon glyphicon-warning-sign'></span>&nbsp;<span" + style + ">" + state + "</span>");
        return;
    } else if (data.name == "" || data.name == null) {
        state = "请填写姓名";
        $("#div").html("<span style='color: #f8c815' class='glyphicon glyphicon-warning-sign'></span>&nbsp;<span" + style + ">" + state + "</span>");
        return;
    } else if (!(/^[\u4e00-\u9fa5]{0,}$/.test(name))) {
        state = "请正确填写姓名";
        $("#div").html("<span style='color: #f8c815' class='glyphicon glyphicon-warning-sign'></span>&nbsp;<span" + style + ">" + state + "</span>");
        return;
    } else if (role == undefined || role == null || role == "") {
        state = "请选择角色";
        $("#div").html("<span style='color: #f8c815' class='glyphicon glyphicon-warning-sign'></span>&nbsp;<span" + style + ">" + state + "</span>");
        return;
    } else if (sex == undefined || sex == null || sex == "") {
        state = "请选择性别";
        $("#div").html("<span style='color: #f8c815' class='glyphicon glyphicon-warning-sign'></span>&nbsp;<span" + style + ">" + state + "</span>");
        return;
    } else if (department == null) {
        state = "请选择科室";
        $("#div").html("<span style='color: #f8c815' class='glyphicon glyphicon-warning-sign'></span>&nbsp;<span" + style + ">" + state + "</span>");
        return;
    } else {
        state = null;
        $("#div").html("<span></span>");
    }
    //删除科室信息
    delete overallSituation.responsibleDepartment;
    //删除角色信息
    delete overallSituation.roles;
    //删除性别信息
    delete overallSituation.sex;
    //添加性别
    overallSituation["sex"] = sex;
    if (state == null) {
        $.ajax({
            url: "http://localhost:8989/hospital/User/Update",
            type: "post",
            dataType: "json",
            data: overallSituation,
            success: function (state) {
                if (state == true) {
                    $.ajax({
                        url: "http://localhost:8989/hospital/UserRole/Update",
                        type: "post",
                        dataType: "json",
                        data: {
                            "userId": overallSituation.id,
                            "roleId": role
                        },
                        success: function (state) {
                            if (state == true) {
                                $.ajax({
                                    url: "http://localhost:8989/hospital/UserDepartment/UpdateDepartment",
                                    type: "post",
                                    dataType: "json",
                                    data: {
                                        "userId": overallSituation.id,
                                        "departmentId": department
                                    },
                                    success: function (state) {
                                        if (state == true) {
                                            this.state = "修改成功";
                                            $("#column").html("<span style='color: #00cc03'>" + this.state + "</span>");
                                            $("#all").attr("checked", false);
                                            $("#myModal2").modal("hide");
                                            //获取angular的controller 调用方法
                                            var $scope = angular.element('#quanXianView').scope();
                                            //调用查询方法
                                            $scope.SelectAll();
                                            setTimeout(function () {
                                                $("#column").empty();
                                            }, 5000)
                                        } else {
                                            this.state = "修改失败";
                                            $("#div").html("<div " + style + "></div>&nbsp;" + "<span " + styles + ">" + this.state + "</span>");
                                        }
                                    }
                                });
                            }
                        }
                    });
                }
            }
        });
    }
}

function leftOperation() {
    //错误信息
    let state;
    //style样式
    let style = " style='color: red'";
    //class值
    let attribute = ".authorized2:selected";
    //选中的值
    let selectedVal = $(attribute).val();
    //判断
    if (selectedVal == undefined) {
        state = "请正确操作";
        $("#div1").html("<span style='color: #f8c815' class='glyphicon glyphicon-warning-sign'></span>&nbsp;<span" + style + ">" + state + "</span>");
        return;
    } else {
        state = "";
        $("#div1").html("<span></span>");
        $(attribute).each(function () {
            let text = $(this).html();
            let id = $(this).val();
            $("#authorizedUser").append("<option class='authorized1' value=" + id + ">" + text + "</option>");
            $("#selectedAuthorizedUser .authorized2:selected").remove();
        });
    }
}

function rightOperation() {
    //错误信息
    let state;
    //style样式
    let style = " style='color: red'";
    //class值
    let attribute = ".authorized1:selected";
    //选中的值
    let selectedVal = $(attribute).val();
    //判断
    if (selectedVal == undefined) {
        state = "请正确操作";
        $("#div1").html("<span style='color: #f8c815' class='glyphicon glyphicon-warning-sign'></span>&nbsp;<span" + style + ">" + state + "</span>");
        return;
    } else {
        state = "";
        $("#div1").html("<span></span>");
        $(attribute).each(function () {
            let text = $(this).html();
            let id = $(this).val();
            $("#selectedAuthorizedUser").append("<option class='authorized2' value=" + id + ">" + text + "</option>");
            $("#authorizedUser .authorized1:selected").remove();
        });
    }
}

function search() {
    //错误信息
    let state;
    //style样式
    let style = " style='color: red'";
    //获取姓名
    let name = $("#myName").val();
    //科室
    let department = $(".option1:selected").text();
    //正则表达式判断
    if ((department == null || department == undefined || department == "") && (name == null || name == "")) {
        state = "请至少选择/填写一个条件";
        $("#div1").html("<span style='color: #f8c815' class='glyphicon glyphicon-warning-sign'></span>&nbsp;<span" + style + ">" + state + "</span>");
        return;
    } else if (!(/^[\u4e00-\u9fa5]{0,}$/.test(name))) {
        state = "请正确填写姓名";
        $("#div1").html("<span style='color: #f8c815' class='glyphicon glyphicon-warning-sign'></span>&nbsp;<span" + style + ">" + state + "</span>");
        return;
    } else {
        state = "搜索成功";
        $("#div1").html("<span style='color: #32db17' class='glyphicon glyphicon-warning-sign'></span>&nbsp;<span style='color: #2aabd2'>" + state + "</span>");
    }
    let data = {
        name: name,
        department: department
    }
    $.ajax({
        url: "http://localhost:8989/hospital/UserDepartment/SelectByNameDepartment",
        type: "post",
        data: data,
        success: function (data) {
            $("#authorizedUser").empty();
            for (let dataKey in data) {
                $("#authorizedUser").append("<option class='authorized1' value=" + data[dataKey].id + ">" + data[dataKey].name + " / " + data[dataKey].id + "</option>");
            }
            if (data.length == 0) {
                $("#authorizedUser").append("<option style='color: red'>无人员</option>");
            }
        }
    });
}